<?php

class M_menu extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function menu_list($search_text, $start, $end){

		$sql="SELECT
				menu.id,
				menu,
				link,
				parent,
				level
			FROM
				menu
			WHERE menu.id IS NOT NULL
			";
		if($search_text != ''){
			$sql .= " AND lower(menu) LIKE '%".$search_text."%' ";
		}
		$sql .= " ORDER by urut ASC ";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}
		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}


	function menu_save($params){
		extract($params);
		if($task == 'create'){
			$this->db->insert('menu',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('menu',$data);
		}
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

	function menu_delete($id){
		$this->db->where('id',$id);
		$this->db->delete('menu');
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}